The present disclosure relates generally to optimizing software, and more particularly to identifying calls in compiled interpreted source code to create memory objects and replacing those calls with calls the reduce the number of objects created.
One of the most important developments in software design has been portable code which allows a developer to write an application in a common language which is then converted into byte code. Java is an example of a popular language that produced byte code. The byte code can be deployed on different machines that have different resources and operating systems by use of a virtual machine environment on those different machines. The byte code is processed by the virtual machine which interacts with the local operating system in the execution of the code to achieve the intended result. During execution, memory resources are managed as needed to allow operation of the code. Managing memory is critically important to the performance and reliability of a data processing system such as a computer. Specifically, data used by a computer program is typically stored in a computer within a memory that has a limited address space. In many computers, data is stored in the form of “objects” that are allocated space in a portion of the memory referred to as an “object heap”.
In many computer environments, objects are created, or “allocated”, dynamically (i.e., on an as-needed basis) during the execution of a computer program. In addition, given that the amount of memory available in a computer is limited, some mechanism for removing, or “deallocating”, unused objects is also provided, typically either through the use of specific program instructions or through an automated process known as garbage collection. One well known computer programming environment that utilizes dynamic memory allocation, for example, is the Java programming environment developed originally by Sun Microsystems, the trademark for which is now owned by Oracle Corp.
One primary benefit of dynamic memory allocation is that the overall amount of memory required to run a computer program is typically reduced, often reducing the memory requirements of a computer upon which the program is executed, or permitting more computer programs to share a given memory space in the computer. Computer performance may also benefit due to the reduced memory requirements, since less swapping of data into and out of memory may be required. Another benefit is that overall reliability of the computer is often improved since memory usage is typically better managed.
The primary drawback to dynamic memory management, however, is the additional “overhead” that is associated with both allocating and deallocating objects. Allocating and deallocating an object each require the computer to perform certain operations that are outside of the actual productive operations that are being performed during execution of a computer program. These additional operations delay the performance of the productive operations, which typically slows the overall operation of the computer. Therefore, for performance concerns, it is highly desirable to minimize whenever possible the number of object allocation and deallocation operations performed by a computer program.
The overhead associated with object allocation and deallocation is of particular concern with respect to computer operations that rely on “temporary” data storage. In particular, with objects that are used repeatedly over a significant period of time, the overhead associated with allocating and deallocating those objects becomes relatively insignificant compared to the productive operations that are performed with those objects. In contrast, whenever objects are only used for a brief period of time, the overhead associated with allocating and then deallocating the objects becomes more significant relative to the productive operations performed with those objects. Furthermore, although the overhead associated with each allocation and deallocation may be relatively minor, when the effects of a large number of operations that rely on temporary objects are considered, the collective impact on overall computer performance can be substantial.
A skilled computer programmer can in some instances reduce the number of object allocation and deallocation operations in a computer program during development of the computer program. In other instances, however, the programmer may not be able to control when some objects are allocated and/or deallocated. For example, a programmer writing a computer program in a source code, or human-readable format, may need to use certain instructions that, when translated into a machine-readable or intermediate format, inherently result in the creation of temporary objects due to the design of the particular translation program (e.g., a compiler or interpreter) used to translate the computer program to the machine-readable or intermediate format.
One such example is a string concatenation operation in the Java programming environment. A string concatenation operation, which is designated by the “+” operator in Java, creates a new string from the two objects identified as arguments to the operation. For example, the concatenation of a first string such as “start” with a second string such as “le” would result in the creation of a new string “startle”.
Compilation of a string concatenation statement in a Java source code program by a Java compiler results in the generation of program code that utilizes a temporary mutable string object, known in Java as a “StringBuffer” object (java.lang.StringBuffer or the unsynchronized equivalent java.lang.StringBuilder which was introduced in Java 5), in performing the string concatenation operation. In addition, other objects are created as well, including a character array for each string buffer object, a String object that gets passed as an argument, and a character array for each String object. Most of these objects are used only once, and in a iterative process of string concatenation, each iteration results in additional one-time only use objects being created. All of these objects persist until a garbage collection operation is performed, which can restrict memory resources until the garbage collection occurs.
Accordingly, there is a need for a way to perform string concatenations without creating so many one-time use string objects.
Those skilled in the field of the present disclosure will appreciate that elements in the figures are illustrated for simplicity and clarity and have not necessarily been drawn to scale. For example, the dimensions of some of the elements in the figures may be exaggerated relative to other elements to help to improve understanding of embodiments of the present invention.
The apparatus and method components have been represented where appropriate by conventional symbols in the drawings, showing only those specific details that are pertinent to understanding the embodiments of the present invention so as not to obscure the disclosure with details that will be readily apparent to those of ordinary skill in the art having the benefit of the description herein. The details of well-known elements, structure, or processes that would be necessary to practice the embodiments, and that would be well known to those of skill in the art, are not necessarily shown and should be assumed to be present unless otherwise indicated.